package com.example.administrator.testc.LoadBitmap;/**
 * Created by Administrator on 2016/5/29 0029.
 */

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class MyHttp extends Thread{
    private String urls;
    private iHttpStream iHttpStream;

    public MyHttp(String url, iHttpStream iHttpStream){
        this.iHttpStream = iHttpStream;
        this.urls = url;
        init();
    }

    @Override
    public void run() {
        super.run();
        try {
            URL url=new URL(urls);//实例化URL对象url
            HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();//实例化HttpURLConnection对象connection
            httpURLConnection.setConnectTimeout(5000);//设置连接超时时间为5秒
            httpURLConnection.setRequestMethod("GET");//设置请求方法为get方式
            if (httpURLConnection.getResponseCode() == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();
                if(iHttpStream != null){
                    iHttpStream.onSuccess(BitmapFactory.decodeStream(inputStream));
                }
                inputStream.close();
            } else{
                Log.e("TAGSS", "404************");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void init(){
        this.start();
    }

    public interface iHttpStream{
        void onSuccess(Bitmap inputStream);
    }
}
